var protocol0 = window.location.protocol;
var protocol = 'http:';
var domain = {
    'api': protocol0 + '//i.fe80.cn', //应用域名
    'upyun': protocol + '//cdn.fe80.cn', //又拍云CDN地址
    'aliyun': protocol + '//oss.i.fe80.cn', //阿里云CDN地址
    'tencent': protocol + '//cos.xinxiaoyi.cn', //腾讯云CDN地址
};

var tool = {};
/**
 * 时间戳转换为日期时间
 * @param int inputTime 时间戳
 * @param n 倍数：1000时间戳，1毫秒级时间戳
 * @param format 格式：date返回日期，datetime返回日期时间
 * @return string 返回自然时间
 */
tool.stamp2datetime = function (inputTime, n = 1000, format = 'datetime') {
    var date = new Date(inputTime * n);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    if (format == 'date') return y + '-' + m + '-' + d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
}

/**
 * 文件大小
 * @param int size 文件数字大小B
 * @return string 返回可视文件大小
 */
tool.filesize = function(size){
    size = parseInt(size);
    if(size > 1048576){
        return (size / 1048576).toFixed(2) + 'MB';
    }
    if (size > 1024){
        return (size / 1024).toFixed(2) + 'KB';
    }
    if (size < 1){
        return '';
    }
    return size + 'B';
}

/**
 * 获取url中的参数 获取url的page参数=>useage：tool.getUrlParam("page")
 * @param num string
 * @return string 返回URL的value
 */
tool.getUrlParam = function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg); //匹配目标参数
    if (r != null) return unescape(r[2]);
    return null; //返回参数值
}

// 字典转换
tool.directory = function(name){
    let dicts = {
        'xinxi': '高中信息技术',
        'files': '文件集',
        'blog': '博客资源',
        'images': '图片资源',
        'res': '资源',
        'tiyu': '体育大数据',
        'uploads': '又拍云图床',
        'video': '视频',
        'jiaocai': '教材(信息技术)',
        'jiaoan': '教案(信息技术)',
        'jiegouhua': '结构化面试资料',
        'jiaoyu': '教育知识与能力',
        'zonghe': '综合知识',
        'app_soft': '应用软件(信息技术)',
        'public': '公用文件',
    };
    let _name = name;
    $.each(dicts,function(index,item){
        if(index == name){
            _name = dicts[index];
        }
    });
    return _name;
}

var path = tool.getUrlParam('path');

if (path == null){
    path = '/';
}

var filedomain = domain.upyun;
var api = domain.api + '/tools/sdk/upyun.html';
var push = { method: 'read', read: path };


$.post(api, push, function (res) {
    renderList(res.read.files, push.read);
});

function renderList (il, path = '') {
    var d = document.getElementById('cdata');
    var _N = '',_F = '';
    for (var i = 0; i < il.length; i++) {
        // 文件夹
        if (il[i].type == 'F') {
            _F += '<tr>' +
                '<td><a class="octicon file-directory" href="?path=' + path + il[i].name + '/">' + tool.directory(il[i].name) + '</a></td>' +
                '<td>' + tool.stamp2datetime(il[i].time) + '</td>' +
                '<td>' + tool.filesize(il[i].size) + '</td>' +
                '</tr>';
        }
        // 文件
        if (il[i].type == 'N') {
            _N += '<tr>'+
                '<td><a class="octicon file-media" target="_blank" href="' + filedomain + path + il[i].name + '">' + il[i].name + '</a></td>'+
                '<td>' + tool.stamp2datetime(il[i].time)+'</td>'+
                '<td>' + tool.filesize(il[i].size)+'</td>'+
                '</tr>';
        }
    }
    let dotdot = '';
    if(path != '/' && path.length > 3){
        let strings = path.split('/');
        if(strings.length > 2){
            let s = strings.slice(0, strings.length - 2);
            dotdot = s.join('/') + '/';
            dotdot = '<tr><td><a class="octicon file-directory" href="?path=' + dotdot + '">..</a></td><td></td><td></td></tr>';
        }
    }
    d.innerHTML =
        '<tr><td><a href="javascript:sortby(0)" class="octicon arrow-up">Name</a></td><td><a href="javascript:sortby(1)" class="octicon arrow-up">Date</a></td><td><a href="javascript:sortby(2)" class="octicon arrow-up">Size</a></td></tr>'
        + dotdot + _F + _N;
    document.getElementById('path').innerHTML = path;
    document.title = 'Index of ' + path;
}